Grokking Python Fundamentals
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
Course Overview
Python Basics
Python Overview
Python Application Areas
Your First Python Program
Python - Basic Syntax
Python Comments
Quiz
Python Variables and Operators
Python - Variables
Python - Data types
Python - Numbers
Python - Booleans
Python - Type Conversions
Python - Operators
Quiz
Python Conditionals
Introduction to Conditional Statements in Python
Python - If...Else
Python - Match Case
Quiz
Python loops
Introduction to loops in Python
Python - for loop
Python - while loop
Python - Loop Control
Python - for else and while else loop
Quiz
Python Strings
Introduction to Strings in Python
Python - String Operators
Python Slicing Strings
Python - String Methods
Quiz
Python Array
Introduction to Array in Python
Python - Array Methods
Quiz
Python Lists
Introduction to Lists in Python
Python - Add and Remove List Items
Python - List Comprehension
Python - List Methods
Quiz
Python Tuples
Introduction to Tuple in Python
Python - Unpack Tuple Items
Python - Join Tuples
Python - Tuple Methods
Quiz
Python -Dictionaries
Introduction to Dictionary in Python
Python - Nested Dictionaries
Python - Dictionary Methods
Quiz
Python -Sets
Introduction to set in Python
Python - set Operations
Python - Set Methods
Quiz
Introductions to Function in Python
Python - Function Arguments
Recursive Functions
Python - Variable scope
Importing Modules
Quiz
Introduction to set in Python

Python sets are collections that are both unordered and unindexed. They are used to store multiple items in a single variable. Sets are particularly useful for performing mathematical operations like unions, intersections, differences, and symmetric differences as only unique elements are stored.

Image

Defining a Set

Sets in Python can be defined by enclosing elements within curly braces {} or by using the built-in set() function. A set automatically removes any duplicate entries, keeping only unique items.

Example: Creating a set

Python3

. . . .

Explanation:

  • fruit_set is defined with duplicates, which are removed when the set is created, resulting in the set {'banana', 'cherry', 'apple'}.
  • color_set uses the set() function with a list that includes duplicates, which are also removed upon creation.

Updating a Set

Sets are mutable, meaning you can add items after the set has been created. The add() method adds a single element, and the update() method can add multiple elements (from another set, list, or any iterable).

Example: Adding elements to a set

Python3

. . . .

Explanation:

  • num_set is initialized with {1, 2, 3}.
  • num_set.add(4) adds the number 4 to the set.
  • num_set.update([5, 6]) adds both 5 and 6 to the set, demonstrating the use of update() for adding multiple items.

Removing Items from a Set

Items can be removed from a set using methods like remove() or discard(). While remove() will raise an error if the item does not exist, discard() will not.

Example: Removing elements from a set

Python3

. . . .

Explanation:

  • my_set starts with {10, 20, 30, 40, 50}.
  • my_set.remove(10) removes 10 from the set.
  • my_set.discard(60) attempts to discard 60. Since 60 is not in the set, nothing happens and no error is thrown.

Copying a Set

To make a copy of a set, you can use the copy() method. This creates a new set that is a copy of the original, allowing you to modify one set without affecting the other.

Example: Copying a set

Python3

. . . .

Explanation:

  • original_set contains {'a', 'b', 'c'}.
  • copied_set = original_set.copy() creates a copy of original_set, resulting in another set with the same elements.

Looping Through a Set

Although sets are unordered, you can still loop through the set items using a for loop. This can be useful for applying operations to each item.

Example: Iterating over a set.

Python3

. . . .

Explanation:

  • char_set is initialized with the characters {'X', 'Y', 'Z'}.
  • The for loop iterates through each element in the set, printing them one by one. The output order may vary due to the unordered nature of sets.

This introduction to Python sets covers the basics of creating and manipulating sets, including adding and removing items, copying sets, and iterating over them. These operations form the foundation of set usage in Python, suitable for managing unique collections of items efficiently.

Quiz
Python - set Operations
Mark as Completed

On this page

Defining a Set

Example: Creating a set

Updating a Set

Example: Adding elements to a set

Removing Items from a Set

Example: Removing elements from a set

Copying a Set

Example: Copying a set

Looping Through a Set

Example: Iterating over a set.